/**
 * @param {number[]} gas
 * @param {number[]} cost
 * @return {number}
 */

var canCompleteCircuit = function (gas, cost) {
    var start = 0;
    var rest = 0;
    var total = 0;
    for (var i = 0; i < gas.length; i++) {
        // 如果总油量小于总消耗的油量
        total = total + gas[i] - cost[i];
        // 剩余的油量
        rest = rest + gas[i] - cost[i];
        // 如果剩余的油量小于0
        if (rest < 0) {
            start = i + 1;
            rest = 0;
        }
    }
    return total < 0 ? -1 : start;
};